import wepy from 'wepy'

export default class extends wepy.mixin {
  data = {
    // 获取商品列表数据参数
    queryData: {
      query: '',
      cid: '',
      pagenum: 1,
      pagesize: 10
    },
    // 商品列表数据
    goods: [],
    // 总条数
    total: 0,
    none: 'http://img95.699pic.com/photo/40052/8563.jpg_wh300.jpg',
    isLoading: false,
    isOver: true
  }

  methods = {
    goDetail (id) {
      wepy.navigateTo ({
        url: '/pages/goods_detail/main?goods_id=' + id
      })
    }
  }
  async getGoodsList (fn) {
    this.isLoading = true
    const {data:res} = await wepy.get('/goods/search',this.queryData)
    if (res.meta.status !== 200) return wepy.baseToast()
    this.goods.push(...res.message.goods)
    this.total = res.message.total
    this.isLoading = false
    fn && fn()
    this.$apply()
  }
  onLoad (options) {
    this.queryData.cid = options.cat_id ? options.cat_id : ''
    this.queryData.query = options.query ? options.query : ''
    this.getGoodsList()
  }

  // 监听上拉触底
  onReachBottom () {
    if (this.isLoading) return
    if (this.queryData.pagenum * this.queryData.pagesize >= this.total) return this.isOver = false
    this.queryData.pagenum++
    this.getGoodsList()
  }
  // 监听下拉刷新
  onPullDownRefresh () {
    this.queryData.pagenum = 1
    this.goods = []
    this.isOver = true
    this.getGoodsList(function () {
      wepy.stopPullDownRefresh()
    })
  }
}
